package org.fitweb.fixture;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitWebElement;

import java.util.ArrayList;
import java.util.List;

public class FitwebElement {

    private HtmlUnitWebElement element;


    public FitwebElement(WebElement element) {
        if (! (element instanceof HtmlUnitWebElement) ) {
            throw new org.fitweb.fixture.DisplayableException("This method is only available when using the HtmlUnit browser");
        }

        this.element = (HtmlUnitWebElement) element;
    }

    public String getName() {
        return getAttribute("name");
    }

    // TODO - CAS - Apr 2, 2010 - Add handling for select boxes
    public String getValue() {
        return element.getValue();
    }

    private String getAttribute(String attributeName) {
        return element.getAttribute(attributeName);
    }

    static List<FitwebElement> asFitwebElements(List<WebElement> inputs) {
        ArrayList<FitwebElement> results = new ArrayList<FitwebElement>();
        for (WebElement input : inputs) {
            results.add(new FitwebElement(input));
        }
        return results;
    }
}
